package mbms.mobilebudget.creditCard;

import java.util.List;

import mbms.mobilebudget.R;
import mbms.mobilebudget.UserManager;
import mbms.mobilebudget.database.DatabaseHandler;
import mbms.mobilebudget.entity.CreditCard;
import mbms.mobilebudget.entity.User;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.j256.ormlite.android.apptools.OrmLiteBaseListActivity;

@SuppressLint("ShowToast")
public class CreditCardsActivity extends OrmLiteBaseListActivity<DatabaseHandler> {
	private User user;
    private CreditCardAdapter creditCardArrayAdapter;
	private UserManager userManager;
	private CreditCardManager creditCardManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_credit_cards);
		
		Intent intent = getIntent();
		user = (User) intent.getSerializableExtra("loggedInUser");
		if(user==null){
			Toast.makeText(getApplicationContext(), 
					"User not acquired", Toast.LENGTH_LONG).show();
			return ;
		}
		
		userManager = new UserManager(getHelper(), getApplicationContext(), user);
		creditCardManager = new CreditCardManager(getHelper(), getApplicationContext());
		List<CreditCard> userCreditCardList = userManager.getCreditCardList();
		
		creditCardArrayAdapter = new CreditCardAdapter(this, R.layout.credit_card_row, userCreditCardList);
		setListAdapter(creditCardArrayAdapter);
		
		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				CreditCard creditCard = creditCardArrayAdapter.getItem(position);
				
				startCreditCardsAddUpdateActivity(creditCard);
			}
		});
		
		registerForContextMenu(getListView());
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.bank_accounts, menu);
		return true;
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	    super.onCreateContextMenu(menu, v, menuInfo);
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.context_menu, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
      CreditCard creditCard = creditCardArrayAdapter.getItem(info.position);

      switch(item.getItemId()) {
      case R.id.edit:
    	  	startCreditCardsAddUpdateActivity(creditCard);
            return true;
      case R.id.delete:
    	    creditCardManager.deleteCreditCard(creditCard);
    	    creditCardArrayAdapter.remove(creditCard);
    	    setListAdapter(creditCardArrayAdapter);
    	    Toast.makeText(getApplicationContext(), "Delete successful", Toast.LENGTH_SHORT).show();
    	    return true;
      default:
            return super.onContextItemSelected(item);
      }
    }
	
	public void showInsertCreditCardActivity(View view) {
		CreditCard creditCard = new CreditCard();
		creditCard.setUser(user);
		
		startCreditCardsAddUpdateActivity(creditCard);
	}
	
	public void backButtonClicked(View view){
		super.onBackPressed();
	} 
	
	private void startCreditCardsAddUpdateActivity(CreditCard creditCard) {
		Intent intent = new Intent(this, CreditCardsAddUpdateActivity.class);
		intent.putExtra("creditCard", creditCard);
		startActivity(intent);
	}
	 @Override
	    public void onResume()
	    {
	        super.onResume();
	 		List<CreditCard> userCreditCards = userManager.getCreditCardList();
			
	 		creditCardArrayAdapter = new CreditCardAdapter(this, R.layout.credit_card_row,userCreditCards );
	 		setListAdapter(creditCardArrayAdapter);
	 		
	 		getListView().setOnItemClickListener(new OnItemClickListener() {
	 			@Override
	 			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
	 				CreditCard creditCard = creditCardArrayAdapter.getItem(position);
	 				
	 				startCreditCardsAddUpdateActivity(creditCard);
	 			}
	 		});
	 		
	 		registerForContextMenu(getListView());
	    }
}
